package com.os.lambda.chapter7;

import com.os.lambda.chapter5.Album;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;


public class Chapter7 {

    public static void main(String[] args) {

    }

    /**
     * 将字符串转换为大写形式
     */
    public static List<String> allToUpperCase(List<String> words){
        return words.stream()
                .map(string->string.toUpperCase())
                .collect(Collectors.<String>toList());
    }

    /**
     * 测试大写转换
     */
    @Test
    public void multipleWordsToUpperCase(){
        List<String> input = Arrays.asList("a", "b", "hello");
        List<String> result = allToUpperCase(input);
        System.out.println(result);
        assertEquals(Arrays.asList("A", "B", "HELLO"), result);
    }

    /**
     * 将列表中元素的第一个字母转换为大写
     */
    public static List<String> elementFirstToUpperCaseLambdas(List<String> words){
        return words.stream()
                .map(value->{
                    char firstChar = Character.toUpperCase(value.charAt(0));
                    return firstChar + value.substring(1);
                })
                .collect(Collectors.toList());
    }

    /**
     * 测试字符串包含两个字符的情况，第一个字母被转换为大写
     */
    @Test
    public void twoLetterStringConvertedToUppercaseLambdas(){
        List<String> input = Arrays.asList("ab");
        List<String> result = elementFirstToUpperCaseLambdas(input);
        assertEquals(Arrays.asList("Ab"), result);
    }

    public static String firstToUpperCase(String value){
        char firstChar = Character.toUpperCase(value.charAt(0));
        return firstChar + value.substring(1);
    }

    /**
     * 将首字母转换为大写，应用到所有列表元素
     */
    public static List<String> elementFirstToUpperCase(List<String> words){
        return words.stream()
                .map(Chapter7::firstToUpperCase)
                .collect(Collectors.toList());
    }

    @Test
    public void twoLetterStringConvertedToUpperCase(){
        String input = "ab";
        String result = firstToUpperCase(input);
        assertEquals("Ab", result);
    }

    @Test
    public void testPeek(){
        List<Album> albums = Arrays.asList(new Album("Matt"),
                new Album("Jack"), new Album("Book"), new Album("Make"),
                new Album("Made"));
        Set<String> names =
                albums.parallelStream()
                .filter(album->album.getName().startsWith("M"))
                .map(album -> album.getName())
                .peek(name->{
                    System.out.println("found name: " + name);
                })
                .collect(Collectors.toSet());

    }


}
